﻿using System;
using System.Diagnostics.Tracing;
using System.Threading;

namespace Sustainalytics.ClientDW.Tracing
{
    [EventSource(Name = "Sustainalytics-ClientDW")]
    public sealed class ClientDWEventSource : EventSource
    {
        private static readonly Lazy<ClientDWEventSource> LazyLog = new Lazy<ClientDWEventSource>(InitLog, LazyThreadSafetyMode.ExecutionAndPublication);

        private ClientDWEventSource() { }

        public static ClientDWEventSource Log
        {
            get
            {
                return LazyLog.Value;
            }
        }

        [NonEvent]
        private static ClientDWEventSource InitLog()
        {
            var log = new ClientDWEventSource();
            log.Ping();


            return log;
        }

        [Event(1, Level = EventLevel.LogAlways, Message = "Ping")]
        public void Ping()
        {
            if (IsEnabled())
                WriteEvent(1);
        }

        [Event(2, Level = EventLevel.Informational, Message = "{0}")]
        public void Message(string message)
        {
            if (IsEnabled())
                WriteEvent(2, message);
        }

        [Event(3, Level = EventLevel.Error, Message = "{0}")]
        public void Error(string error)
        {
            if (IsEnabled())
                WriteEvent(3, error);
        }

        [Event(4, Level = EventLevel.Informational, Opcode = EventOpcode.Start, Message = "SyncEntity<{0}>")]
        public void StartSyncEntity(string entityType)
        {
            if (IsEnabled())
                WriteEvent(4, entityType);
        }

        [Event(5, Level = EventLevel.Informational, Opcode = EventOpcode.Stop, Message = "SyncEntity<{0}> RowsAffected: {1}")]
        public void StopSyncEntity(string entityType, int rowsAffected)
        {
            if (IsEnabled())
                WriteEvent(5, entityType);
        }

        [Event(6, Level = EventLevel.Informational, Message = "SaveDocument<{0}>, RowsAffected: {1}, ElapsedTime: {2}")]
        public void SaveDocuments(string documentType, int rowsAffected, double elapsedTime)
        {
            if (IsEnabled())
                WriteEvent(6, documentType, rowsAffected, elapsedTime);
        }
    }
}
